import Vue from 'vue'
import Vuex from 'vuex'


Vue.use(Vuex)
    //types
const types = {
    SET_LOCATION: "SET_LOCATION", //定位信息
    SET_ADDRESS: "SET_ADDRESS", //地址 
    USER_ADDRESS: "USER_ADDRESS", //用户写的收货地址
    ORDER_INFO: "ORDER_INFO", //用户购物车的商品
    REMARK_INFO: "REMARK_INFO",
};
//state,相当于组件中的data，专门用来存放全局数据
const state = {
    location: {},
    address: "",
    user_address: null,
    orderInfo: null,
    remarkInfo: {
        tableWare: "",
        remark: "",
    },
    userLogin: '',
};
//getters 相当于组件中的computed，不过getters是全局的，computed是组件内的
const getters = {
    location: state => state.location, //获取定位信息
    address: state => state.address, //获取地址 
    orderInfo: state => state.orderInfo,
    //计算总价
    totalPrice: state => {
        let price = 0;
        if (state.orderInfo) {
            const selectFood = state.orderInfo.selectFood;
            selectFood.forEach(item => {
                price += (item.activity.fixed_price * item.count);
            });
            //再加上配送费
            price += state.orderInfo.shopInfo.float_delivery_fee
        }
        return price;
    },
    remarkInfo: state => state.remarkInfo,
    userLogin: state => state.userLogin,
    user_address: state => state.user_address, //用户的收货地址
};
//mutations 相当于组件中的methods，但他不能使用异步方法（定时器、axios）由commit调用
const mutations = {
    [types.SET_LOCATION](state, location) {
        if (location) {
            state.location = location
        } else {
            state.location = null
        }
    },
    [types.SET_ADDRESS](state, address) {
        if (address) {
            state.address = address
        } else {
            //如果地址不存在就为空 一开始的状态就定义为空
            state.address = ""
        }
    },
    [types.USER_ADDRESS](state, user_address) {
        if (user_address) {
            state.user_address = user_address;
        } else {
            //如果地址不存在就为空 一开始的状态就定义为空
            state.user_address = null
        }
    },
    [types.ORDER_INFO](state, orderInfo) {
        if (orderInfo) {
            state.orderInfo = orderInfo
        } else {
            state.orderInfo = null
        }
    },
    [types.REMARK_INFO](state, remarkInfo) {
        if (remarkInfo) {
            state.remarkInfo = remarkInfo
        } else {
            state.remarkInfo = null
        }
    },
    addUserLogin(state, payload) {
        state.userLogin = payload;
    },
    [types.USER_ADDRESS](state, user_address) {
        if (user_address) {
            state.user_address = user_address
        } else {
            state.ruser_address = null
        }
    },
};
//actions 是store中专门用来处理异步的 由dispatch调用  调用实际修改状态值的，还是mutations 
const actions = {
    setLocation: ({ commit }, location) => {
        commit(types.SET_LOCATION, location);
    },
    setAddress: ({ commit }, address) => {
        commit(types.SET_ADDRESS, address)
    },
    setUserAddress: ({ commit }, user_address) => {
        commit(types.USER_ADDRESS, user_address)
    },
    orderInfo: ({ commit }, orderInfo) => {
        commit(types.ORDER_INFO, orderInfo)
    },
    remarkInfo: ({ commit }, remarkInfo) => {
        commit(types.REMARK_INFO, remarkInfo)
    },
}
export default new Vuex.Store({
    state,
    getters,
    mutations,
    actions
})